package com.example.tally;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.tally.db.DBManager;

import java.io.IOException;

public class SettingActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
    }
    public void onClick(View v){
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.setting_iv_back:
                finish();
                break;
            case R.id.setting_tv_clean:
                showDeleteDialog();
                break;
            case R.id.setting_data_import:
                importDataFromTxt();
                break;
            case R.id.setting_data_export:
                exportDataToTxt();
                break;
            case R.id.setting_ty_manager:
                intent.setClass(this, TypeBeanActivity.class);
                this.startActivity(intent);
                break;
        }
    }

    private void exportDataToTxt() {
        //导出数据到txt文件
        new AlertDialog.Builder(this).setTitle("导出提示")
                .setMessage("您确要导出数据吗")
                .setNegativeButton("取消" , null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBManager.exportDataToTxt();
                        Toast.makeText(SettingActivity.this , "导出成功" , Toast.LENGTH_SHORT).show();
                    }
                }).create().show();
    }

    private void importDataFromTxt(){
        new AlertDialog.Builder(this).setTitle("导入提示")
                .setMessage("您确要导入数据吗？ \n注意：导入之前会清空原始数据，请谨慎操作")
                .setNegativeButton("取消" , null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String result = "";
                        try {
                            result = DBManager.importDatafromTxt();
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        Toast.makeText(SettingActivity.this , result , Toast.LENGTH_SHORT).show();
                    }
                }).create().show();
    }

    private void showDeleteDialog() {
        new AlertDialog.Builder(this).setTitle("删除提示")
                .setMessage("您确要清空数据吗？ \n注意：清空后无法恢复，请慎重选择")
                .setNegativeButton("取消" , null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBManager.deleteAllAccounttb();
                        Toast.makeText(SettingActivity.this , "清空成功" , Toast.LENGTH_SHORT).show();
                    }
                }).create().show();
    }
}